#include "LayoutParameter.h"
#include "xVMRelationStruct.h"
#include "TextLineGUI.h"
#include "ComboBoxGUI.h"

#include <QBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QWidget>
#include <QComboBox>

using namespace std;

LayoutParameter::LayoutParameter(string id, string name, string type, string value)
{
	_id = id;
	_name = name;
	_type = type;
	_value = value;
	_pGUI = NULL;
	if (type == "textarea"||type == "vm")
	{
		_pGUI = new TextLineGUI;
	}
	else if (type == "selectbox")
	{
		_pGUI = new ComboBoxGUI;
	}
	else assert(false);		//please select right type
}


LayoutParameter::LayoutParameter(string id, string name, string type, vector<string> str)
{
	_id = id;
	_name = name;
	_type = type;
	_pGUI = NULL;
	ComboBoxGUI* combo = new ComboBoxGUI();
	for(int i=0; i<str.size();++i)
	{
		combo->insertItem(i, QString::fromStdString(str.at(i)));
		combo->setMaximumWidth(100);
	}

	_pGUI = combo;
}

void LayoutParameter::setParameterValue(string value)
{
	_value = value;
}

string LayoutParameter::getParameterID()
{
	return _id;
}

string LayoutParameter::getParameterValue()
{
	_value = _pGUI->getValue();
	return _value;
}

vector<DIVA_VM*> LayoutParameter::getParameterVM()
{
	_VM = xVMRelationStruct::getInstance()->VMRS_searchVMByVarname(_value);
	return _VM;
}

ParameterGUI* LayoutParameter::getParameterGUI()
{
	return _pGUI;
}

QBoxLayout* LayoutParameter::getGUI()
{
	QHBoxLayout *parameterLayout = new QHBoxLayout();
	QLabel *label = new QLabel();
	if(_type == "textarea" || _type == "vm")
	{
		/*
		editarea->addEvent( "change", [this]()->void {
			this->_pGUI;
		} );
		*/
		TextLineGUI* editarea = dynamic_cast<TextLineGUI*>(_pGUI);
		editarea->setMaximumWidth(100);
		label->setText(QString::fromStdString(_name));
		parameterLayout->addWidget(label);
		parameterLayout->addWidget(editarea);
		editarea->setText(QString::fromStdString(_value));
	}
	else if (_type == "selectbox")
	{
		ComboBoxGUI* selectbox = dynamic_cast<ComboBoxGUI*>(_pGUI);
		label->setText(QString::fromStdString(_name));
		parameterLayout->addWidget(label);
		parameterLayout->addWidget(dynamic_cast<QWidget*>(selectbox));
	}
	else
		return NULL;
	_gui = parameterLayout;
	return _gui;
}

void LayoutParameter::setValue()
{
	if(_type == "vm" || _type == "VM")
	{
		QLineEdit* editarea = dynamic_cast<QLineEdit*>(_pGUI);
		_value = editarea->text().toUtf8().constData();
		_VM = xVMRelationStruct::getInstance()->VMRS_searchVMByVarname(_value);
	}
	else
	{
		_value = _pGUI->getValue();
	}

}

void LayoutParameter::setSelectBoxValue(vector<string> str)
{
	ComboBoxGUI* combo = new ComboBoxGUI();
	for(int i=0; i<str.size();++i)
	{
		combo->insertItem(i, QString::fromStdString(str.at(i)));
		combo->setMaximumWidth(100);
	}

	_pGUI = combo;
}

void LayoutParameter::setSelectBoxValue(DIVA_VM* VM)
{
	vector<string> str = xVMRelationStruct::getInstance()->get_subVMsVarname(VM);
	ComboBoxGUI* combo = new ComboBoxGUI();
	for(int i=0; i<str.size();++i)
	{
		combo->insertItem(i, QString::fromStdString(str.at(i)));
		combo->setMaximumWidth(100);
	}

	_pGUI = combo;

}